﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventWatcher {
    public partial class MainForm : Form {
        public MainForm() {
            InitializeComponent();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
            oAboutBox1 about = new oAboutBox1();
            about.ShowDialog(this);
        }

        private void newSingleToolStripMenuItem_Click(object sender, EventArgs e) {
            Input input = new Input();
            DialogResult result = input.ShowDialog(this);
            if (result == DialogResult.OK) {
                Child child = new Child(input.textBox1.Text.Trim());
                child.Text = input.textBox1.Text.Trim();
                child.MdiParent = this;
                //have the child show its self when it's good n darn ready!
            }
        }

        private void newListToolStripMenuItem_Click(object sender, EventArgs e) {            
            Input input = new Input();
            DialogResult result = input.ShowDialog(this);
            if (result == DialogResult.OK) {
                this.toolStripProgressBar.Visible = true;
                this.toolStripProgressBar.Value = 1;
                SpawnFromList(BuildList(input.textBox1.Text.Trim()));
            }
        }

        List<string> BuildList(string path) {
            List<string> results = new List<string>();
            using (System.IO.StreamReader sr = new System.IO.StreamReader(path)) {
                string line;
                while ((line = sr.ReadLine()) != null)
                    if (line.Length > 2)
                        results.Add(line);
            }
            return results;
        }

        void SpawnFromList(List<string> machines) {
            this.timer1.Enabled = true;
            Beans.MaxValue = machines.Count;
            this.toolStripProgressBar.Maximum = Beans.MaxValue;

            foreach (string machine in machines) {
                Child child = new Child(machine);
                child.Text = machine;
                child.MdiParent = this;
                System.Threading.Thread.Sleep(10);
            }
        }

        private void timer1_Tick(object sender, EventArgs e) {
            if (Beans.Loaded >= this.toolStripProgressBar.Maximum) {
                this.toolStripProgressBar.Maximum = Beans.Loaded;
            }
            this.toolStripProgressBar.Value = Beans.Loaded;
            if (Beans.Loaded >= Beans.MaxValue) {
                this.toolStripProgressBar.Visible = false;
                this.timer1.Enabled = false;
            }
        }

    }
}
